package cn.passlink.bletoothchat.SqlTables;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.passlink.bletoothchat.Entity.SendMessage;

public class MessageTableManager {

    private SQLiteDatabase database;

    public MessageTableManager(SQLiteDatabase database) {
        this.database = database;
    }

    public void addMessage(SendMessage message) {
        ContentValues values = new ContentValues();
        values.put("sender", message.getSender());
        values.put("academy", message.getAcademy());
        values.put("grade", message.getGrade());
        values.put("classes", message.getClasses());
        values.put("receiver", message.getReceiver());
        values.put("send_time", message.getSend_time());
        values.put("receiver_time", message.getReceive_time());
        values.put("effective","1");
        values.put("effec_time", message.getEffec_time());
        values.put("message", message.getMessage());
        values.put("location", message.getLocation());

        database.insert("message", null, values);
    }

    public void deleteMessage(int messageId) {
        String whereClause = "id" + " = ?";
        String[] whereArgs = {String.valueOf(messageId)};
        database.delete("message", whereClause, whereArgs);
    }

    public void updateMessage(SendMessage message,String sendTime) {
        ContentValues values = new ContentValues();
        values.put("sender", message.getSender());
        values.put("academy", message.getAcademy());
        values.put("grade", message.getGrade());
        values.put("classes", message.getClasses());
        values.put("receiver", message.getReceiver());
        values.put("send_time", message.getSend_time());
        values.put("receiver_time", message.getReceive_time());
        values.put("effective", message.getEffective());
        values.put("effec_time", message.getEffec_time());
        values.put("message", message.getMessage());
        values.put("location", message.getLocation());
        String whereClause = "send_time" + " = ?";
        String[] whereArgs = {String.valueOf(message.getSend_time())};
        database.update("message", values, whereClause, whereArgs);
    }

    public static String getCurrentDateTime() {
        // 创建 SimpleDateFormat 实例，指定日期时间格式
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前日期和时间
        Date now = new Date();
        // 格式化日期和时间
        String formattedDateTime = format.format(now);
        // 返回结果
        return formattedDateTime;
    }

    public List<SendMessage> getAllMessageBySenderAndReceiver(String sender, String receiver) {
        List<SendMessage> messageList = new ArrayList<>();

        String[] columns = null;
        String selection = "(sender = ? AND receiver = ?) OR (receiver = ? AND sender = ?) AND effective = 1";
        String[] selectionArgs = {sender, receiver,receiver, sender};
        String orderBy = "send_time ASC";

        Cursor cursor = database.query("message", columns, selection, selectionArgs, null, null, orderBy);
        while (cursor.moveToNext()) {
            SendMessage message = new SendMessage();
            message.setAcademy(cursor.getString(cursor.getColumnIndexOrThrow("academy")));
            message.setGrade(cursor.getString(cursor.getColumnIndexOrThrow("grade")));
            message.setSender(cursor.getString(cursor.getColumnIndexOrThrow("classes")));
            message.setSender(cursor.getString(cursor.getColumnIndexOrThrow("sender")));
            message.setReceiver(cursor.getString(cursor.getColumnIndexOrThrow("receiver")));
            message.setSend_time(cursor.getString(cursor.getColumnIndexOrThrow("send_time")));
            message.setReceive_time(cursor.getString(cursor.getColumnIndexOrThrow("receiver_time")));
            message.setEffective(cursor.getInt(cursor.getColumnIndexOrThrow("effective")));
            message.setEffec_time(cursor.getString(cursor.getColumnIndexOrThrow("effec_time")));
            message.setMessage(cursor.getString(cursor.getColumnIndexOrThrow("message")));
            message.setLocation(cursor.getString(cursor.getColumnIndexOrThrow("location")));
            messageList.add(message);
            ///BUG
            // 根据条件更新effective字段
//            long diff = calculateTimeDifference( message.getSend_time());
//            if (diff >= Long.parseLong(message.getEffec_time())) {
//                message.setEffective(0);
////                updateMessage(message,message.getSend_time());
//            } else {
//                message.setEffective(1);
////                updateMessage(message,message.getSend_time());
//
//            }
        }

        cursor.close();
        return messageList;
    }

    // 计算时间差
    private long calculateTimeDifference( String sendTime) {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date nowDate = new Date();
            String nowtimeString = format.format(nowDate);
            Date   nowtime  = format.parse(nowtimeString);
            Date sendDate = format.parse(sendTime);
            long timeDiffMillis = nowtime.getTime() - sendDate.getTime();
            return timeDiffMillis / (60 * 60 * 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
}